<?php

namespace App\depository;

use App\Models\ManageCMDModel;

class ManageCMDDep
{
    public $model;

    public function __construct(){
        $this->model = new ManageCMDModel();
    }

    public function add($data){
        $res = $this->model->insert($data);
        return $res;
    }

    public function list($param){
        $res = $this->model
            ->where('team_id', $param['team_id'])
            ->when(!empty($param['ids']),function($query) use ($param){
                $query->where("ids",'like',"%{$param['ids']}%");
            })
            ->when(!empty($param['ip']),function($query) use ($param){
                $query->where("ip",'like',"%{$param['ip']}%");
            })
            ->when(!empty($param['content']),function($query) use ($param){
                $query->where("content",'like',"%{$param['content']}%");
            })
            ->when(!empty($param['date'][0]),function($query) use($param){
                return $query->where("date",">=",$param['date'][0]);
            })
            ->when(!empty($param['date'][1]),function($query) use($param){
                return $query->where("date","<=",$param['date'][1]);
            })
            ->orderBy('id', 'desc')
            ->paginate($param['per_page'], ['*'], 'page', $param['current_page']);;
        return $res;
    }
}
